<template>
  <div class="article-detail-page">
    <front-header></front-header>
    
    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
      <div class="leaf leaf-1"></div>
      <div class="leaf leaf-2"></div>
    </div>
    
    <div class="main-content" v-loading="loading">
      <!-- 文章内容区 -->
      <div class="article-container" v-if="article">
        <h1 class="article-title">{{ article.title }}</h1>
        
        <!-- 文章摘要 -->
        <div class="article-summary" v-if="article.summary">
          <i class="el-icon-info-filled"></i>
          {{ article.summary }}
        </div>
        
        <div class="article-meta">
          <div class="meta-left">
            <span class="publish-time">
              <i class="el-icon-time"></i>
              {{ formatTime(article.createdAt) }}
            </span>
            <span class="view-count">
              <i class="el-icon-view"></i>
              {{ article.viewCount }} 阅读
            </span>
          </div>
          <div class="meta-right">
            <el-button type="success" plain size="small" icon="el-icon-back" @click="$router.push('/articles')">
              返回列表
            </el-button>
          </div>
        </div>

        <!-- 封面图 -->
        <div class="cover-image" v-if="article.coverImage">
          <el-image 
            :src="article.coverImage?.startsWith('http') ? article.coverImage : `/api${article.coverImage}`"
            fit="cover">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </div>

        <!-- 文章内容 -->
        <div class="article-content" v-html="article.content"></div>
        
        <!-- 文章底部 -->
        <div class="article-footer">
          <div class="share-buttons">
            <span class="share-label">分享到：</span>
            <el-button type="text" icon="el-icon-share" circle></el-button>
            <el-button type="text" icon="el-icon-chat-dot-round" circle></el-button>
            <el-button type="text" icon="el-icon-star-off" circle></el-button>
          </div>
          <el-button type="primary" icon="el-icon-back" @click="$router.push('/articles')">
            返回文章列表
          </el-button>
        </div>
      </div>

      <!-- 空状态 -->
      <div class="empty-container" v-else-if="!loading">
        <el-empty description="文章不存在或已被删除">
          <el-button type="primary" @click="$router.push('/articles')">返回列表</el-button>
        </el-empty>
      </div>
    </div>
    <front-footer></front-footer>
  </div>
</template>

<script>
import FrontHeader from '@/components/front/FrontHeader.vue'
import FrontFooter from '@/components/front/FrontFooter.vue'
import Request from '@/utils/request'
import { formatTime } from '@/utils/time'

export default {
  name: 'ArticleDetail',
  components: {
    FrontHeader,
    FrontFooter
  },
  data() {
    return {
      loading: false,
      article: null
    }
  },
  created() {
    this.getArticleDetail()
  },
  methods: {
    formatTime,
    async getArticleDetail() {
      const id = this.$route.params.id
      if (!id) {
        this.$router.push('/articles')
        return
      }

      this.loading = true
      try {
        const res = await Request.get(`/article/${id}`)
        if (res.code === '0') {
          this.article = res.data
          // 更新浏览量
          // await Request.put(`/article/${id}/view`)
          // this.article.viewCount++
        } else {
          this.$message.error(res.msg||'获取文章详情失败')
        }
      } catch (error) {
        console.error('获取文章详情失败:', error)
        this.$message.error(error.message||'获取文章详情失败')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.article-detail-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #fff8e1 0%, #fff3e0 100%);
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, rgba(255, 152, 0, 0.1), rgba(255, 193, 7, 0.05));
}

.circle-1 {
  width: 600px;
  height: 600px;
  top: -200px;
  right: -100px;
}

.circle-2 {
  width: 400px;
  height: 400px;
  bottom: -100px;
  left: -100px;
}

.circle-3 {
  width: 300px;
  height: 300px;
  top: 40%;
  right: 10%;
  background: linear-gradient(135deg, rgba(255, 193, 7, 0.05), rgba(255, 152, 0, 0.08));
}

.leaf {
  position: absolute;
  background-color: rgba(255, 152, 0, 0.1);
  border-radius: 50% 0;
  transform: rotate(45deg);
}

.leaf-1 {
  width: 200px;
  height: 200px;
  bottom: 10%;
  right: 5%;
}

.leaf-2 {
  width: 150px;
  height: 150px;
  top: 20%;
  left: 5%;
  transform: rotate(-30deg);
}

.main-content {
  flex: 1;
  padding: 32px;
  max-width: 1000px;
  margin: 0 auto;
  width: 100%;
  box-sizing: border-box;
  position: relative;
  z-index: 2;
}

.article-container {
  background: white;
  padding: 40px;
  border-radius: 16px;
  border: 1px solid rgba(255, 193, 7, 0.1);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.03);
  position: relative;
  overflow: hidden;
}

.article-title {
  font-size: 32px;
  font-weight: 600;
  color: #F57C00;
  margin: 0 0 24px 0;
  line-height: 1.4;
}

.article-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  margin-bottom: 24px;
  background: rgba(255, 193, 7, 0.05);
  border-radius: 12px;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.meta-left {
  display: flex;
  gap: 20px;
  color: #606266;
  font-size: 14px;
}

.publish-time, .view-count {
  display: flex;
  align-items: center;
  gap: 6px;
}

.publish-time i, .view-count i {
  color: #FF9800;
}

.meta-right :deep(.el-button) {
  font-size: 14px;
}

.meta-right :deep(.el-button:hover) {
  color: #FF9800;
}

.cover-image {
  margin-bottom: 32px;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid rgba(255, 193, 7, 0.1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.cover-image:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(255, 152, 0, 0.1);
}

.cover-image :deep(.el-image) {
  width: 100%;
  max-height: 450px;
}

.article-content {
  line-height: 1.8;
  color: #303133;
  font-size: 16px;
  padding: 0 24px;
}

/* 取消 scoped 以便样式能作用于富文本内容 */
.article-content :deep(.w-e-text-container) {
  background: transparent;
  border: none;
}

.article-content :deep(img) {
  max-width: 100%;
  height: auto !important;
  border-radius: 8px;
  margin: 16px 0;
  display: block;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.article-content :deep(p) {
  margin: 16px 0;
  line-height: 1.8;
}

.article-content :deep(h1) { font-size: 28px; margin: 28px 0 18px; }
.article-content :deep(h2) { font-size: 24px; margin: 24px 0 16px; }
.article-content :deep(h3) { font-size: 20px; margin: 20px 0 14px; }
.article-content :deep(h4) { font-size: 18px; margin: 18px 0 12px; }
.article-content :deep(h5) { font-size: 16px; margin: 16px 0 10px; }
.article-content :deep(h6) { font-size: 15px; margin: 15px 0 10px; }

.article-content :deep(h1),
.article-content :deep(h2),
.article-content :deep(h3),
.article-content :deep(h4),
.article-content :deep(h5),
.article-content :deep(h6) {
  color: #F57C00;
  font-weight: 600;
  line-height: 1.4;
}

.article-content :deep(a) {
  color: #FF9800;
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: all 0.3s;
}

.article-content :deep(a:hover) {
  border-bottom-color: #FF9800;
}

.article-content :deep(blockquote) {
  margin: 16px 0;
  padding: 16px 24px;
  background: rgba(255, 193, 7, 0.05);
  border-left: 4px solid #FFC107;
  color: #606266;
  border-radius: 0 8px 8px 0;
}

.article-content :deep(pre) {
  background: #f8f9fa;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 16px 0;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.article-content :deep(code) {
  background: rgba(255, 193, 7, 0.05);
  padding: 2px 6px;
  border-radius: 4px;
  color: #FF9800;
  font-family: Consolas, Monaco, 'Andale Mono', monospace;
}

.article-content :deep(ul),
.article-content :deep(ol) {
  padding-left: 24px;
  margin: 16px 0;
}

.article-content :deep(li) {
  margin: 8px 0;
  line-height: 1.6;
}

.article-content :deep(table) {
  border-collapse: collapse;
  width: 100%;
  margin: 16px 0;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
}

.article-content :deep(th),
.article-content :deep(td) {
  border: 1px solid rgba(255, 193, 7, 0.1);
  padding: 12px;
  text-align: left;
}

.article-content :deep(th) {
  background-color: rgba(255, 193, 7, 0.05);
  font-weight: 500;
  color: #F57C00;
}

.article-content :deep(tr:nth-child(even)) {
  background-color: rgba(255, 193, 7, 0.02);
}

.article-content :deep(hr) {
  border: none;
  border-top: 1px solid rgba(255, 193, 7, 0.1);
  margin: 24px 0;
}

/* 代码块样式优化 */
.article-content :deep(pre code) {
  background: transparent;
  padding: 0;
  color: inherit;
  display: block;
}

/* 图片说明文字样式 */
.article-content :deep(figure) {
  margin: 16px 0;
  text-align: center;
}

.article-content :deep(figcaption) {
  color: #606266;
  font-size: 14px;
  margin-top: 8px;
  font-style: italic;
}

/* 摘要样式 */
.article-summary {
  background: rgba(255, 193, 7, 0.05);
  padding: 20px 24px;
  border-radius: 12px;
  margin: 0 0 24px 0;
  color: #606266;
  font-size: 16px;
  line-height: 1.6;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  position: relative;
  border: 1px solid rgba(255, 193, 7, 0.1);
}

.article-summary i {
  color: #FF9800;
  font-size: 18px;
  margin-top: 3px;
}

/* 文章底部样式 */
.article-footer {
  margin-top: 40px;
  padding-top: 24px;
  border-top: 1px solid rgba(255, 193, 7, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.share-buttons {
  display: flex;
  align-items: center;
  gap: 8px;
}

.share-label {
  color: #606266;
  font-size: 14px;
}

.share-buttons :deep(.el-button) {
  color: #FF9800;
  font-size: 18px;
}

.share-buttons :deep(.el-button:hover) {
  color: #F57C00;
}

.article-footer :deep(.el-button--primary) {
  background: linear-gradient(135deg, #FF9800, #FFC107);
  border: none;
}

/* 空状态容器 */
.empty-container {
  background: white;
  padding: 40px;
  border-radius: 16px;
  border: 1px solid rgba(255, 193, 7, 0.1);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.03);
  text-align: center;
}

.empty-container :deep(.el-empty__image) {
  width: 120px;
  height: 120px;
}

.empty-container :deep(.el-button--primary) {
  background: linear-gradient(135deg, #FF9800, #FFC107);
  border: none;
  padding: 12px 24px;
  font-size: 15px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .main-content {
    padding: 16px;
  }

  .article-container {
    padding: 24px;
  }

  .article-title {
    font-size: 24px;
  }

  .article-meta {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }

  .meta-right {
    align-self: flex-end;
  }

  .article-content {
    padding: 0;
  }

  .article-footer {
    flex-direction: column;
    gap: 20px;
  }

  .share-buttons {
    width: 100%;
    justify-content: center;
  }

  .article-footer :deep(.el-button--primary) {
    width: 100%;
  }
}
</style>